package ModelLayer;
import java.util.ArrayList;
/**
 * Container for Customer objects
 * 
 * @author (Gruppe 3)
 */
public class CustomerContainer
{
    private ArrayList<Customer> customers;
    private static CustomerContainer instance;
    private int index;
    
    /**
     * constructor
     */
    private CustomerContainer()
    {
        customers = new ArrayList<Customer>();
        index = 0;
    }
    
    /**
     * singleton
     */
    public static CustomerContainer getInstance()
    {
        if(instance == null)
        {
            instance = new CustomerContainer();
        }
        return instance;
    }
    
    /**
     * Adds a customer to the ArrayList customers 
     * @param name          name of the customer to be added
     * @param address       address of the customer to be added
     * @param phone         phone of the customer to be added
     * @param email         email of the customer to be added
     * @param customerID    id of the customer to be added
     * @param discount      discount of the customer to be added
     * @param cvrNr         CVR number of the customer to be added
     */
    public void addCustomer(String name, String address, String phone, String email, String customerID, double discount, String cvrNr)
    {
        Customer newcustomer = new Customer(name, address, phone, email, customerID, discount, cvrNr);
        customers.add(newcustomer);
    }
    
    /**
     * Finds a customer in the ArrayList customers
     * @param searchID
     * @return true, if customer was found, otherwise returns false
     */
    public boolean findCustomer(String searchID)
    {
        index = 0;
        boolean found = false;
        while (!found && index < customers.size())
        {
            Customer id = customers.get(index);
            if(id.getCustomerID().equals(searchID))
            {
                found = true;
                return found;
            }
            else{
                index++;
            }
            
        }
        return found;
    }
    
    /**
     * Deletes a customer in ArrayList customers
     * @param ID  id of the wanted customer to be deleted
     */
    public void deleteCustomer(String ID)
    {
        findCustomer(ID);
        customers.remove(index);
    }
    
    /**
     * Updates a customer in the ArrayList customers
     * @param name          name of the customer to be updated
     * @param address       address of the customer to be updated
     * @param phone         phone of the customer to be updated
     * @param email         email of the customer to be updated
     * @param customerID    id of the customer to be updated
     * @param discount      discount of the customer to be updated
     * @param cvrNr         CVR number of the customer to be updated
     * @param ID            old id of the customer to be updated
     */
    public void updateCustomer(String name, String address, String phone, String email, String customerID, double discount, String cvrNr, String ID)
    {
        findCustomer(ID);
        customers.get(index).setName(name);
        customers.get(index).setPhone(phone);
        customers.get(index).setAddress(address);
        customers.get(index).setEmail(email);
        customers.get(index).setCustomerID(customerID);
        customers.get(index).setDiscount(discount);
        customers.get(index).setCvrNr(cvrNr);
    }
    
    /**
     * prints a customer's fields as output to console
     * @param ID  id of the customer to be printed
     */
    public void listCustomer(String ID)
    {
        findCustomer(ID);
        System.out.println("Name: " + customers.get(index).getName());
        System.out.println("Address: " + customers.get(index).getAddress());
        System.out.println("Phone: " + customers.get(index).getPhone());
        System.out.println("Email: " + customers.get(index).getEmail());
        System.out.println("CustomerID: " + customers.get(index).getCustomerID());
        System.out.println("Discount: " + customers.get(index).getDiscount());
        System.out.println("CvrNr: " + customers.get(index).getCvrNr());        
    }
    
    /**
     * list all customers in the ArrayList customers
     */
    public void listAllCustomers()
    {
        for(Customer c : customers){
            System.out.println("Name: " + c.getName());
            System.out.println("Address: " + c.getAddress());
            System.out.println("Phone: " + c.getPhone());
            System.out.println("Email: " + c.getEmail());
            System.out.println("CustomerID: " + c.getCustomerID());
            System.out.println("Discount: " + c.getDiscount());
            System.out.println("CvrNr: " + c.getCvrNr());
            System.out.println();
        }
        
    }
    
    /**
     * Finds and returns a customer 
     * @param searchID 
     * @return found customer, otherwise returns null
     */
    public Customer getCustomer(String searchID)
    {
        if (findCustomer(searchID))
        {
            return customers.get(index);
        }
        
        return null;
    }
}